package com.dsit.sq;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.dsit.sq.mydefineview.CircleProgressView;
import com.dsit.sq.mydefineview.PanView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DecimalFormat;

public class MainActivity extends BaseActivity implements OnClickListener {

    private ImageView settingImageView;
    private PanView mPanView;
    private TextView chargePercentTextView;
    private ImageView efImageView;
    private ImageView chargeImageView;
    private ImageView qingImageView;
    private TextView readyTextView;
    private TextView qingPercentTextView;
    private CircleProgressView progressView;
    private CircleProgressView qingProgressView;
    private final int chargePercent = 0;
    private final int qingPercent = 7;
    private final int fuelBatterySystemFault = 22;
    private final int addHOne = 30;
    private final int addHTwo = 31;
    private final int faults = 32;
    private final int eOrF = 33;
    private final int ready = 34;
    private final int chargeIndication = 35;
    private final int hLeakAlarm = 36;
    //三个温度值
    private final int mcInvTemp = 37;
    private final int mctemp = 38;
    private final int dcf_temp = 19;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarBackChange.setStatusBarBack(this);
        settingImageView = (ImageView) findViewById(R.id.setting_image);
        settingImageView.setOnClickListener(this);
        mPanView = (PanView) findViewById(R.id.pan_view);
        mPanView.setBackgroundColor(Color.TRANSPARENT);
        mPanView.setOnClickListener(this);
        efImageView = (ImageView) findViewById(R.id.ef_image);
        chargeImageView = (ImageView) findViewById(R.id.charge_image);
        qingImageView = (ImageView) findViewById(R.id.qing_image);
        readyTextView = (TextView) findViewById(R.id.ready_text);

        chargePercentTextView = (TextView) findViewById(R.id.charge_percent);
        qingPercentTextView = (TextView) findViewById(R.id.qing_percent);
        progressView = (CircleProgressView) findViewById(R.id.progress_view);
        qingProgressView = (CircleProgressView) findViewById(R.id.qing_progress_view);
        qingProgressView.progressColorId = R.color.colorDeepBlue;
        qingProgressView.seekIconId = R.mipmap.bg_light_blue_03;
        qingProgressView.isClockWise = false;
        qingProgressView.startAngel = 140;
        qingProgressView.totalAngel = 100;

        ProtoMsgDispatcher.getInstance().addDataListener(dataListener);
    }

    // TcpClient tcpClient;
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ProtoMsgDispatcher.getInstance().removeDataListener(dataListener);
        App.instance.tcpClientSocketThread.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_image:
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
                break;
            case R.id.pan_view:
                Intent carIntent = new Intent(this, CarDataActivity.class);
                startActivity(carIntent);
                break;
            default:
                break;
        }
    }

    /*
    最下边按钮的onTouch事件
     */
//    @Override
//    public boolean onTouch(View v, MotionEvent event) {
//        switch (v.getId()) {
//            case R.id.cars_data_image:
//                if (event.getAction() == MotionEvent.ACTION_DOWN) {
//                    carDataImageView.setBackgroundResource(R.mipmap.btn_shuju_press);
//                } else if (event.getAction() == MotionEvent.ACTION_UP) {
//                    carDataImageView.setBackgroundResource(R.mipmap.btn_shuju_nor);
//                }
//                break;
//            default:
//                break;
//        }
//        return false;
//    }

    private DataListener dataListener = new DataListener() {
        @Override
        public void onUpdateData(ProtoMessage msg) {
            if(msg.getActionId().equals("canbus.msg")){
                byte[] data = msg.getContentData();
                Log.i("main_activity", "收到回调");
                String str = new String(data, 0, data.length);
                try {
                    JSONArray array = new JSONArray(str);
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = (JSONObject) array.get(i);
                        int id = object.getInt("id");
                        Double value = object.getDouble("value");
                        Bundle bundle = new Bundle();
                        Message message = new Message();
                        message.setData(bundle);
                        bundle.putDouble("value", value);
                        switch (id) {
                            case chargePercent:
                                message.what = chargePercent;
                                mHandler.sendMessage(message);
                                break;
                            case qingPercent:
                                message.what = qingPercent;
                                mHandler.sendMessage(message);
                                break;
                            case fuelBatterySystemFault:
                                message.what = fuelBatterySystemFault;
                                mHandler.sendMessage(message);
                                break;
                            case addHOne:
                                message.what = addHOne;
                                mHandler.sendMessage(message);
                                break;
                            case addHTwo:
                                message.what = addHTwo;
                                mHandler.sendMessage(message);
                                break;
                            case faults:
                                message.what = faults;
                                mHandler.sendMessage(message);
                                break;
                            case eOrF:
                                message.what = eOrF;
                                mHandler.sendMessage(message);
                                break;
                            case ready:
                                message.what = ready;
                                mHandler.sendMessage(message);
                                break;
                            case hLeakAlarm:
                                message.what = hLeakAlarm;
                                mHandler.sendMessage(message);
                                break;
                            case chargeIndication:
                                message.what = chargeIndication;
                                mHandler.sendMessage(message);
                                break;
                            case mcInvTemp:
                                message.what = mcInvTemp;
                                mHandler.sendMessage(message);
                                break;
                            case mctemp:
                                message.what = mctemp;
                                mHandler.sendMessage(message);
                                break;
                            case dcf_temp:
                                message.what = dcf_temp;
                                mHandler.sendMessage(message);
                                break;
                            default:
                                break;
                        }

                        Log.i("data", object.toString());

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    };
    private boolean qingOne = false;
    private boolean qingTwo = false;
    private boolean isOverOne = false;
    private boolean isOverTwo = false;
    private boolean isOverThreee = false;
    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message message) {
            try {
                Bundle bundle = new Bundle();
                bundle = message.getData();
                switch (message.what) {
                    case chargePercent:
                        chargePercentTextView.setText(formatDouble(bundle.getDouble("value")) + "%");
                        if (bundle.getDouble("value") - 100.0 > 0) {
                            progressView.setProgress(100);
                            break;
                        }
                        progressView.setProgress((int) bundle.getDouble("value"));
                        break;
                    case qingPercent:
                        qingPercentTextView.setText(formatDouble(bundle.getDouble("value")) + "%");
                        if (bundle.getDouble("value") - 100.0 > 0) {
                            qingProgressView.setProgress(100);
                            break;
                        }
                        qingProgressView.setProgress((int) bundle.getDouble("value"));
                        break;
                    case fuelBatterySystemFault:
                        mPanView.i = 3;
                        if (bundle.getDouble("value") == 1.0 || bundle.getDouble("value") == 2.0) {
                            mPanView.isOnFour = true;
                        } else {
                            mPanView.isOnFour = false;
                        }
                        mPanView.invalidate();
                        break;
                    case addHOne:
                        qingOne = (bundle.getDouble("value") == 1.0) ? true : false;
                        break;
                    case addHTwo:
                        qingTwo = (bundle.getDouble("value") == 1.0) ? true : false;
                        if (qingTwo && qingOne) {
                            qingImageView.setBackgroundResource(R.mipmap.icon_h_on);
                        } else {
                            qingImageView.setBackgroundResource(R.mipmap.icon_h_off);
                        }
                        break;
                    case faults:
                        if (bundle.getDouble("value") == 0.0) {
                            mPanView.i = 6;
                            mPanView.isOnSeven = true;
                        } else {
                            mPanView.i = 6;
                            mPanView.isOnSeven = false;
                        }
                        if (bundle.getDouble("value") == 3.0 || bundle.getDouble("value") == 4.0
                                || bundle.getDouble("value") == 9.0) {
                            mPanView.i = 7;
                            mPanView.isOnEight = true;
                        } else {
                            mPanView.i = 7;
                            mPanView.isOnEight = false;
                        }
                        if (bundle.getDouble("value") == 7.0) {
                            mPanView.i = 1;
                            mPanView.isOnTwo = true;
                        } else {
                            mPanView.i = 1;
                            mPanView.isOnTwo = false;
                        }
                        if (bundle.getDouble("value") == 7.0 || bundle.getDouble("value") == 5.0) {
                            mPanView.i = 0;
                            mPanView.isOnOne = true;
                        } else {
                            mPanView.i = 0;
                            mPanView.isOnOne = false;
                        }
                        mPanView.invalidate();
                        break;
                    case eOrF:
                        if (bundle.getDouble("value") == 10.0) {
                            efImageView.setBackgroundResource(R.mipmap.bg_e_light_on);
                        } else if (bundle.getDouble("value") == 40.0) {
                            efImageView.setBackgroundResource(R.mipmap.bg_f_light_on);
                        } else {
                            efImageView.setBackgroundResource(R.mipmap.bg_ef_light_off);
                        }
                        break;
                    case ready:
                        if (bundle.getDouble("value") == 4.0) {
                            readyTextView.setTextColor(getResources().getColor(R.color.colorWhite));
                        } else {
                            readyTextView.setTextColor(getResources().getColor(R.color.color_gray));
                        }
                        break;
                    case hLeakAlarm:
                        mPanView.i = 4;
                        if (bundle.getDouble("value") == 1.0) {
                            //慢闪
                            if ((mPanView.time == 0)) {
                                mPanView.time = 600;
                                mPanView.startThread();
                            } else {
                                mPanView.time = 600;
                            }
                            mPanView.isOnFive = true;
                        } else if (bundle.getDouble("value") == 2.0) {
                            //快闪
                            if ((mPanView.time == 0)) {
                                mPanView.time = 300;
                                mPanView.startThread();
                            } else {
                                mPanView.time = 300;
                            }
                            mPanView.isOnFive = true;
                        } else if (bundle.getDouble("value") == 3.0) {
                            //常亮
                            mPanView.time = 0;
                            mPanView.isOnFive = true;
                        } else {
                            //暗
                            mPanView.time = 0;
                            mPanView.isOnFive = false;
                        }
                        mPanView.invalidate();
                        break;
                    case chargeIndication:
                        mPanView.i = 5;
                        if (bundle.getDouble("value") == 1.0) {
                            chargeImageView.setBackgroundResource(R.mipmap.icon_dian_on);
                            mPanView.isOnSix = true;
                        } else {
                            chargeImageView.setBackgroundResource(R.mipmap.icon_dian_off);
                            mPanView.isOnSix = false;
                        }
                        mPanView.invalidate();
                        break;
                    case mcInvTemp:
                        mPanView.i = 2;
                        if ((bundle.getDouble("value") - 90.0) > 0.00001) {
                            //大于90度超温
                            isOverOne = true;
                            mPanView.isOnThree = true;
                        } else {
                            isOverOne = false;
                            if (isOverTwo || isOverThreee) {
                                break;
                            }
                            mPanView.isOnThree = false;
                        }
                        mPanView.invalidate();
                        break;
                    case mctemp:
                        mPanView.i = 2;
                        if ((bundle.getDouble("value") - 140.0) > 0.00001) {
                            //大于140度超温
                            isOverTwo = true;
                            mPanView.isOnThree = true;
                        } else {
                            isOverTwo = false;
                            if (isOverOne || isOverThreee) {
                                break;
                            }
                            mPanView.isOnThree = false;
                        }
                        mPanView.invalidate();
                        break;
                    case dcf_temp:
                        mPanView.i = 2;
                        if ((bundle.getDouble("value") - 78.0) > 0.00001) {
                            //大于78度超温
                            isOverThreee = true;
                            mPanView.isOnThree = true;
                        } else {
                            isOverThreee = false;
                            if (isOverOne || isOverTwo) {
                                break;
                            }
                            mPanView.isOnThree = false;
                        }
                        mPanView.invalidate();
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private String formatDouble(Double data) {
        DecimalFormat format = new DecimalFormat("0.00");
        return format.format(data);
    }
}
